#include <SFML/Graphics.hpp>
#include <vector>
#include <math.h>
#include <iostream>
#include "convert.h"
#include "Entities.h"
#include "Manager.h"
#include "Utils.h"
#include "Collision.h"
using namespace std;
using namespace sf;

int main()
{
	RenderWindow App(VideoMode(800, 600, 32), "Undead Debacle v0.0");
	GameManager manager;
	manager.LoadCfg("udconfig.ini");
	vector<string> imageNames;
	imageNames.push_back(PLAYER_IMAGE + IMG_EXT);
	imageNames.push_back(ZOMBIE_IMAGE + IMG_EXT);
	imageNames.push_back(WALL_IMAGE + IMG_EXT);
	manager.LoadContent(imageNames);
	Image bulletImage;
	bulletImage.Create(8, 2, Color(150, 150, 150));
	
	int numZombies, numSceneries = 0;
	Font font;
	if (!font.LoadFromFile("inconsolata.otf"))
	{
		cout << "Failed to load inconsolata.otf" << endl;
		App.Close();
	}
	View v(Vector2f(400, 300), Vector2f(400, 300));
	App.SetView(v);
	App.ShowMouseCursor(false);
	App.UseVerticalSync(true);

	manager.AddEntity(Entity::PLAYER, Vector2f(0, 0));
	Player * player = manager.PlayerPtr();
	//player->initBullets(bulletImage);

	Image iCrosshair = Image();
	iCrosshair.LoadFromFile("crosshair.png");
	Sprite sCrosshair = Sprite(iCrosshair);
	sCrosshair.SetCenter(sCrosshair.GetImage()->GetWidth() / 2, sCrosshair.GetImage()->GetHeight() / 2);
	sCrosshair.SetBlendMode(Blend::Add);
	for (int i = 0; i < Randomizer::Random(5, 20); i++)
	{
		int x = Randomizer::Random(0, App.GetWidth() + 400) - 200;
		int y = Randomizer::Random(0, App.GetHeight() + 400) - 200;
		manager.AddEntity(Entity::WALKABLE, Vector2f(x, y));
		numSceneries++;
	}
	for (int i = 0; i < Randomizer::Random(10, 30); i++)
	{
		int x = Randomizer::Random(0, App.GetWidth() + 400) - 200;
		int y = Randomizer::Random(0, App.GetHeight() + 400) - 200;
		manager.AddEntity(Entity::ENEMY, Vector2f(x, y));
		numZombies++;
	}
	float zoomAmount = 1.0f;

	sf::String sNumZombies("", font, 20), sNumSceneries("", font, 20);
	sNumZombies.Move(20.f, 40.f);
	sNumSceneries.Move(20.f, 60.f);
	const Input & input = App.GetInput();
	while (App.IsOpened())
	{
		numZombies = 0;
		numSceneries = 0;
		Vector2f mousePos = App.ConvertCoords(input.GetMouseX(), input.GetMouseY());
		sCrosshair.SetPosition(mousePos);
		if (player->Alive() == false)
			App.Close();
		Event Event;
		while (App.GetEvent(Event))
		{
			switch (Event.Type)
			{
				case Event::Closed:
					App.Close();
					break;
				case Event::KeyPressed:
					if (Event.Key.Code == Key::Escape)
						App.Close();
					break;
				case Event::MouseWheelMoved:
					zoomAmount = 1.0f + Event.MouseWheel.Delta / 10.0f;
					v.Zoom(zoomAmount);
					break;
				case Event::MouseButtonPressed:
					player->Fire();
					break;
			}
		}

		if (input.IsKeyDown(Key::W))
		{
			player->Move(Vector2f(0, -player->Speed()));
		}

		if (input.IsKeyDown(Key::S))
			player->Move(Vector2f(0, player->Speed()));
		if (input.IsKeyDown(Key::A))
			player->Move(Vector2f(-player->Speed(), 0));
		if (input.IsKeyDown(Key::D))
			player->Move(Vector2f(player->Speed(), 0));
		//player->Target(mousePos);
		
		manager.Update(App.GetFrameTime());

		viewFollow(v, player->Position(), App.GetFrameTime());
		App.Clear();

		manager.Draw(App);

		sNumSceneries.SetText("Sceneries: " + stringify(numSceneries));
		sNumZombies.SetText("Zombies: " + stringify(numZombies));

		sNumSceneries.SetPosition(App.ConvertCoords(10.f, 10.f));
		sNumZombies.SetPosition(App.ConvertCoords(10.f, 30.f));
		
		App.Draw(sNumSceneries);
		App.Draw(sNumZombies);
		App.Draw(sCrosshair);

		App.Display();
	}
	return EXIT_SUCCESS;
}
